﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Manager;
namespace HealthSystem
{
    public partial class Profile : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                GetProile();
                Inite();

                
            }
        }
        private void Inite()
        {
            this.unit.Enabled = false;
            this.address.Enabled = false;
            this.zip.Enabled = false;
            this.linkman.Enabled = false;
            this.phone.Enabled = false;
            this.mobile.Enabled = false;
            this.email.Enabled = false;
            if (CustomerProfile.RoleId == 1001)
            {
                btnMap.Hidden = true;
            }
        }
        private void GetProile()
        {
            if (CustomerProfile.RoleId < 2000 && CustomerProfile.RoleId > 0)
            {
                HW_Manger_Admin_UserManager manager = new HW_Manger_Admin_UserManager();
                Model.HW_Manger_Admin_User user = manager.GetById(CustomerProfile.UserName);
                this.username.Text = user.XX_UserName;
                this.unit.Text = user.hW_Admin_Detail.WorkUnit;
                this.address.Text = user.hW_Admin_Detail.Address;
                this.zip.Text = user.hW_Admin_Detail.ZipCode;
                this.linkman.Text = user.CNName;
                this.phone.Text = user.hW_Admin_Detail.OfficePhone;
                this.mobile.Text = user.hW_Admin_Detail.MobilePhone;
                this.email.Text = user.hW_Admin_Detail.Email;
            }

            if (CustomerProfile.RoleId == 2000)
            {
                HW_OrgUserManager manager = new HW_OrgUserManager();
                Model.HW_OrgUser user = manager.GetById(CustomerProfile.UserName);
                this.username.Text = user.XX_UserName;
                this.unit.Text = user.hw_Org.OrgName;
                this.address.Text = user.hw_Org.hw_OrgDetail.Address;
                this.zip.Text = user.hw_Org.hw_OrgDetail.ZipCode;
                this.linkman.Text = user.CNName;
                this.phone.Text = user.hw_Org.hw_OrgDetail.OfficePhone;
                this.mobile.Text = user.hw_Org.hw_OrgDetail.MobilePhone;
                this.email.Text = user.hw_Org.hw_OrgDetail.Email;
            }
            if (CustomerProfile.RoleId == 3000)
            {
                HW_School_UserManager manager = new HW_School_UserManager();
                Model.HW_School_User user = manager.GetById(CustomerProfile.UserName);
                this.username.Text = user.XX_UserName;
                this.unit.Text = user.hw_School.Name;
                this.address.Text = user.hw_School.Address;
                //this.zip.Text = CustomerProfile.SchoolUser.hw_School.;
                this.linkman.Text = user.CNName;
                this.phone.Text = user.hw_School.OfficePhone;
                this.mobile.Text = user.hw_School.MobilePhone;
                this.email.Text = user.hw_School.Email;
            }
        }
        protected void save_Click(object sender, EventArgs e)
        {
            try
            {
                if (CustomerProfile.RoleId < 2000 && CustomerProfile.RoleId > 0)
                {
                    HW_Manger_Admin_UserManager manager = new HW_Manger_Admin_UserManager();
                    Model.HW_Manger_Admin_User user = manager.GetById(CustomerProfile.UserName);

                    user.CNName = this.linkman.Text;
                    manager.ModHW_Manger_Admin_User(user);


                    user.hW_Admin_Detail.WorkUnit= this.unit.Text ;
                     user.hW_Admin_Detail.Address=this.address.Text;
                    user.hW_Admin_Detail.ZipCode= this.zip.Text;
                    user.hW_Admin_Detail.OfficePhone=this.phone.Text;
                   user.hW_Admin_Detail.MobilePhone= this.mobile.Text;
                    user.hW_Admin_Detail.Email=this.email.Text;

                    HW_Admin_DetailManager detailmanager = new HW_Admin_DetailManager();
                    detailmanager.ModHW_Admin_Detail(user.hW_Admin_Detail);

                }
                if (CustomerProfile.RoleId == 2000)
                {
                    HW_OrgUserManager manager = new HW_OrgUserManager();
                    Model.HW_OrgUser user = manager.GetById(CustomerProfile.UserName);
                    
                  
                    user.CNName = this.linkman.Text;
                    manager.ModHW_OrgUser(user);


                    user.hw_Org.OrgName = this.unit.Text;
                    HW_OrgManager orgmanager = new HW_OrgManager();
                    orgmanager.ModHW_Org(user.hw_Org);


                    user.hw_Org.hw_OrgDetail.Address = this.address.Text;
                    user.hw_Org.hw_OrgDetail.ZipCode = this.zip.Text;
                    user.hw_Org.hw_OrgDetail.OfficePhone = this.phone.Text;
                    user.hw_Org.hw_OrgDetail.MobilePhone = this.mobile.Text;
                    user.hw_Org.hw_OrgDetail.Email = this.email.Text;
                    HW_OrgDetailManager detailmanager = new HW_OrgDetailManager();
                    detailmanager.ModHW_OrgDetail(user.hw_Org.hw_OrgDetail);
                }
                if (CustomerProfile.RoleId == 3000)
                {
                    HW_School_UserManager manager = new HW_School_UserManager();
                    Model.HW_School_User user = manager.GetById(CustomerProfile.UserName);
                    
                    //this.zip.Text = CustomerProfile.SchoolUser.hw_School;
                    user.CNName= this.linkman.Text ;
                    manager.ModHW_School_User(user);

                    HW_SchoolManager schoolmanager = new HW_SchoolManager();
                    user.hw_School.Name = this.unit.Text;
                    user.hw_School.Address = this.address.Text;
                    user.hw_School.OfficePhone = this.phone.Text;
                    user.hw_School.MobilePhone = this.mobile.Text;
                    user.hw_School.Email = this.email.Text;
                    schoolmanager.ModHW_School(user.hw_School);
                };
                GetProile();
                Inite();
                ExtAspNet.Alert.Show("保存成功");
            }
            catch
            {
                ExtAspNet.Alert.Show("保存出错！");
            }

        }
        protected void edit_Click(object sender, EventArgs e)
        {
            this.unit.Enabled = true;
            this.address.Enabled =true;
            this.zip.Enabled = true;
            this.linkman.Enabled = true;
            this.phone.Enabled = true;
            this.mobile.Enabled = true;
            this.email.Enabled = true;
        }
        protected void btnMap_Click(object sender, EventArgs e)
        {
            string skey = string.Empty;
            string stype = string.Empty;
            if (CustomerProfile.RoleId == 2000)
            {
                skey = CustomerProfile.OrgUser.OrgCode;
                stype = "Org";
            }
            else if (CustomerProfile.RoleId == 3000)
            {
                skey = CustomerProfile.SchoolUser.SchoollCode;
                stype = "School";
            }
            string url = "~/Map/webform1.aspx?skey=" + skey + "&stype=" + stype;
            ExtAspNet.PageContext.RegisterStartupScript(Window1.GetShowReference(url, "添加地标"));
        }
    }
}